<template>
	<view>
		<Navbar></Navbar>
		<u-tabs bg-color="transparent" active-color="#B3723E" :current="tabCurrent" :list="tabList" @change="tabChange"></u-tabs>
		<view class="u-m-t-20 u-m-l-30 u-m-r-30">
			<Exchange :data="exchangeData" @cardApply="cardApply" @addPerson="addPerson" @callMobile="callMobile" @goFriendDetail="goFriendDetail" v-if="[0].includes(tabCurrent)"></Exchange>
			<view v-else>
				<view class="u-m-b-30">
					<u-search placeholder="请输入关键词" v-model="keyword" height="80" bg-color="#fff" @search="search" @clear="transfer" @custom="search"></u-search>
				</view>
				<GroupBox ref="friendBox" :groupType="groupType" :list="list" @callMobile="callMobile" @onChooseLabel="onChooseLabel" @goDetail="goDetail"></GroupBox>
			</view>
		</view>
		
		<Modal :show="modal.show" :confirmText="modal.confirm" :modalContent="modal.content" :showCancel="modal.showCancel"
			@confirm="modalConfirm" @cancel="modal.show = false"></Modal>
		
		<RemarkPopup :userRemark="remarkText" :popupShow="remarkShow" @popupClose="remarkShow = false" @confirm="remarkConfirm"></RemarkPopup>
		
		<Loadmore :status="loadStatus"></Loadmore>
		
		<view style="height: 100rpx;"></view>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Exchange from './component/Exchange.vue'
	import GroupBox from '@/components/GroupBox.vue'
	import Modal from '@/components/Modal.vue'
	import Loadmore from '@/components/Loadmore.vue'
	import RemarkPopup from '@/components/RemarkPopup.vue'
	import reachBottom from '@/mixins/reachBottom.js'
	import { PresidentLevel, TeaStatus, call, flattenObj, presidentClass, pointTransform } from '@/utils/index.js'
	import { businessCardExchange, cardSubmit, friendList, userInfo, singleIntegral, businessCardApply, userRelationDel, userRemark } from '@/network/api.js'
	import { teaFriendList } from '@/network/tea.js'
	export default{
		mixins: [reachBottom],
		components:{
			Navbar,
			Exchange,
			GroupBox,
			Modal,
			Loadmore,
			RemarkPopup
		},
		data(){
			return{
				exchangeData:{
					exchangeList:[],
					finishList:[]
				},
				list: [], // 好友 来友列表
				user_id: uni.getStorageSync("user_id"),
				tabCurrent:0,
				tabList:[
					{name:'交换记录'},
					{name:'我的好友'},
					{name:'我的来友'}
				],
				modal: {
					show: false,
					showCancel: true,
					confirm: '确定',
					content: '',
					isDelete: 0 , // 删除好友
					id: ''
				},
				groupType: 0,
				keyword: '',
				fromUserId: 0,
				friendType: 0, // 0 好右 1来友
				remarkShow: false,
				remarkText: '',
				loadStatus: 'loadmore',
			}
		},
		onLoad(){
			this.getBusinessCardExchange()
		},
		methods:{
			tabChange(val){
				this.tabCurrent = val
				this.transfer()
			},
			getBusinessCardExchange(){
				const params = {
					user_id: this.user_id,
					page: this.page,
					limit: this.pageSize
				}
				businessCardExchange(params).then(res=>{
					let exchangeList = []
					let finishList = []
					res.data.map(item=>{
						item.number = pointTransform(item.number)
						item.president_status = parseInt(item.president_status)
						item.president_name = this.getPresidentName(item.partner_status,item.president_status)
						item.president_class = presidentClass(item.president_status)
						if(item.tengxun_address !== null){
							item.address = `${item.tengxun_address.city}${item.tengxun_address.district}${item.address}`
						}
						if(item.is_exchange == "0"){
							exchangeList.push(item)
						}else{
							finishList.push(item)
						}
					})
					this.exchangeData.exchangeList = [...this.exchangeData.exchangeList,...exchangeList]
					this.exchangeData.finishList = [...this.exchangeData.finishList,...finishList]
					
					if(finishList.length < 10) this.loadStatus = 'nomore'
				})
			},
			cardApply(val){
				cardSubmit(val).then(res=>{
					uni.showToast({
						title: `${res.msg}`,
						icon: "none"
					})
					this.exchangeData.exchangeList.length = 0
					this.exchangeData.finishList.length = 0
					this.page = 1
					this.getBusinessCardExchange()
				})
			},
			goFriendDetail(id){
				uni.navigateTo({
					url: "/teaBureau/teaPersonDetail/teaPersonDetail?id=" + id
				})
			},
			addPerson(id){
				this.fromUserId = id
				/* const params = {
					key: "ExchangeCards",
					type: 0
				}
				singleIntegral(params).then(res=>{
					this.modal.content = `交换名片需要消耗${res.data.value}积分，请问是否仍要继续？`
					this.modal.show = true
				}) */
				this.modal.content = `交换名片，对方同意后交换成功`
				this.modal.show = true
			},
			callMobile(val){
				call(val)
			},
			modalConfirm(){
				this.modal.show = false
				if(this.modal.isDelete == 1){
					userRelationDel({card_id: this.modal.id}).then(res=>{
						uni.showToast({
							title: `${res.msg}`,
							icon: "none"
						})
						this.modal.isDelete = 0
						this.list.length = 0
						this.page = 1
						this.getFriendList()
					})
				}else{
					userInfo({user_id:this.user_id}).then(res=>{
						let integral = res.data.integral  // 用户积分
						const params = {
							key: "ExchangeCards",
							type: 0
						}
						singleIntegral(params).then(singleRes=>{
							let singleIntegral = singleRes.data.value
							if(parseInt(integral) < parseInt(singleIntegral)){
								uni.showToast({
									title: "积分不足",
									icon: "none"
								})
							}else{
								const applyParams = {
									user_id: this.user_id,
									from_user_id: this.fromUserId
								}
								businessCardApply(applyParams).then(applyRes=>{
									uni.showToast({
										title: `${applyRes.msg}`,
										icON: "none"
									})
								})
							}
						})
					})
				}
			},
			getFriendList(){
				this.groupType = 1
				const params = {
					user_id: this.user_id,
					page: this.page,
					limit: this.pageSize,
					keyword: this.keyword
				}
				friendList(params).then(res=>{
					this.friendType = 0
					if(res.data.length > 0){
						res.data.map(item=>{
							item.president_status = parseInt(item.president_status)
							item.president_name = this.getPresidentName(item.partner_status,item.president_status)
							item.president_class = presidentClass(item.president_status)
							if(item.tengxun_address !== null){
								item.address = `${item.tengxun_address.city}${item.tengxun_address.district}${item.address}`
							}
							item.label_list = []
							item.evaluate_content = ''
							this.list.push(item)
						})
					}
					if(res.data.length < 10) this.loadStatus = 'nomore'
				})
			},
			getTeaFriendList(){
				this.groupType = 2
				const params = {
					user_id: this.user_id,
					page: this.page,
					limit: this.pageSize,
					keyword: this.keyword
				}
				teaFriendList(params).then(res=>{
					this.friendType = 1
					if(res.data.list.length > 0){
						res.data.list.map(item=>{
							item.user_info.president_name = this.getPresidentName(item.user_info.partner_status,item.user_info.president_status)
							item.user_info.president_class = presidentClass(item.user_info.president_status)
							if(item.user_info.tengxun_address !== null){
								item.user_info.address = `${item.user_info.tengxun_address.city}${item.user_info.tengxun_address.district}${item.user_info.address}`
							}
							let params = {label_list: item.label_list,evaluate_content: item.evaluate_content, data_num: item.data_num}
							item = {...flattenObj(item.user_info),...params}
							this.list.push(item)
						})
					}
					if(res.data.list.length < 10) this.loadStatus = 'nomore'
					
				})
			},
			onChooseLabel(val){
				switch(val.type){
					case 1:
					   this.$refs.friendBox.commonPopup.show = false
					   this.modal.id = val.id
					   this.modal.content = `请确认是否要解除好友关系？`
					   this.modal.isDelete = 1
					   this.modal.show = true
					    break;
					case 2:
					    this.$refs.friendBox.commonPopup.show = false
					    this.modal.id = val.id
					    this.remarkShow = true
						this.remarkText = val.remark
					    break;
				}
			},
			remarkConfirm(val){
				const params = {
					user_id: this.user_id,
					from_user_id: this.modal.id,
					remark: val
				}
				userRemark(params).then(res=>{
					uni.showToast({
						title: `${res.msg}`
					})
					this.remarkShow = false
					this.list = []
					this.page = 1
					if(this.tabCurrent == 1){
						this.getFriendList()
					}else{
						this.getTeaFriendList()
					}
				})
			},
			getPresidentName(status1, status2) {
				// return PresidentLevel(status)
        return TeaStatus(status1, status2)  
			},
			transfer(){
				this.list = []
				this.exchangeData.exchangeList.length = 0
				this.exchangeData.finishList.length = 0
				this.page = 1
				this.keyword = ''
				switch(this.tabCurrent){
					case 0:
					    this.getBusinessCardExchange()
						break;
					case 1:
					    this.getFriendList()
						break;
					case 2:
					    this.getTeaFriendList()
						break;
				}
			},
			search(){
				this.list = []
				this.exchangeData.exchangeList.length = 0
				this.exchangeData.finishList.length = 0
				this.page = 1
				switch(this.tabCurrent){
					case 1:
					    this.getFriendList()
						break;
					case 2:
					    this.getTeaFriendList()
						break;
				}
			},
			loadData() {
				if(this.loadStatus == 'nomore') return
				this.page++
				this.loadStatus = 'loading'
				this.transfer()
			}
		}
	}
</script>

<style>
</style>